package org.programmers.anonymous.fightrobots.dao.impl;

import java.util.List;

import org.programmers.anonymous.fightrobots.dao.IWeaponDao;
import org.programmers.anonymous.fightrobots.model.IWeapon;
import org.programmers.anonymous.fightrobots.model.Weapon;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * Implements the IWeaponDao Interface
 * 
 * @author Daniel Cruz, Francisco Villalba
 *
 */
public class WeaponDao extends HibernateDaoSupport implements IWeaponDao{

	
	@Override
	public int save(IWeapon w) {
		Integer id = null;
		if (Integer.valueOf(w.getIdWeapon()) == -99)
		{
			
			id = (Integer) getHibernateTemplate().save(w);
			w.setIdWeapon(id);
		
		}
		else
		{
			getHibernateTemplate().saveOrUpdate(w);
			id = w.getIdWeapon();
		}
		return id;
	}

	@Override
	public IWeapon loadWeapon(int Id) {
		Weapon w = getHibernateTemplate().get(Weapon.class, Id);
		return w;
	}

	@Override
	public boolean delete(IWeapon w) {
		try {
			getHibernateTemplate().delete(w);
			return true;
		}catch(Exception hibernateException){
			return false;
		}
	}


	
	@Override
	public List<IWeapon> getWeaponList() {
		
		
		
		return  (List<IWeapon>) getHibernateTemplate().find("from Weapon");

//			  List<IWeapon> weaponList = getHibernateTemplate().executeFind(new HibernateCallback<List<IWeapon>>()
//			    {
//				        public List<IWeapon> doInHibernate(Session session) throws HibernateException
//				        {
//				            Query q = session.createQuery("from weapon");
//			            return  Collections.checkedList(q.list(), IWeapon.class);
//			          
//				        }
//				    });
//				    return weaponList;

	}

	@Override
	public void deleteAllWeapons() {
		for (IWeapon weapon : getWeaponList())
		{
			delete(weapon);
		}

	}

}
